﻿namespace com.vs.Inventory.BL
{
    using com.vs.DataAccess;
    using com.vs.DataAccess.DO;
    using com.vs.General;
    using com.vs.General.BL;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class ReplenishmentHelper
    {
        public bool bCreateTask = true;
        private bool bEmptyLocFirst = true;
        private bool bPickAfterRpln = true;
        private int iRplnMode = 0;

        public ReplenishmentHelper(Session sess)
        {
            this.GetReplSysValue(sess);
        }

        public string[] GetEmptyLocs(Session sess, string stWarehouse, string stCompany, string stItem, string stQtyUM)
        {
            DataHelper helper = null;
            DataTable table = null;
            string[] strArray2;
            string cmdText = "WAV_RRplnEmptyLoc";
            string[] strArray = null;
            try
            {
                helper = new DataHelper(sess);
                table = helper.GetTable(CommandType.StoredProcedure, cmdText, new IDataParameter[] { DataHelper.BuildParameter(sess, "@WAREHOUSE", stWarehouse), DataHelper.BuildParameter(sess, "@COMPANY", stCompany), DataHelper.BuildParameter(sess, "@ITEM", stItem), DataHelper.BuildParameter(sess, "@QUANTITY_UM", stQtyUM) });
                if (DataManager.IsEmpty(table))
                {
                    return null;
                }
                strArray = new string[table.Rows.Count];
                int num = 0;
                foreach (DataRow row in table.Rows)
                {
                    strArray[num++] = DataManager.GetString(row, "LOCATION");
                }
                strArray2 = strArray;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                strArray2 = null;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return strArray2;
        }

        public List<ShipmentDetailDO> GetOpenShipmentDetailsInWave(Session sess, int iWaveNum)
        {
            DataHelper helper = null;
            DataTable table = null;
            string cmdText = null;
            List<ShipmentDetailDO> list;
            try
            {
                helper = new DataHelper(sess);
                cmdText = " SELECT * FROM SHIPMENT_DETAIL WHERE (REQUEST_QTY - TOTAL_QTY) > 0 AND INTERNAL_WAVE_NUM = " + iWaveNum;
                table = helper.GetTable(CommandType.Text, cmdText, new IDataParameter[0]);
                if (!DataManager.IsEmpty(table))
                {
                    return ShipmentDetailDO.LoadListFromDataView(table.DefaultView);
                }
                list = null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                list = null;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return list;
        }

        private string[] getPreviousAllocLoc(Session sess, int iWaveNum, string stCompany, string stItem, int iAttributeNum, string stQtyUM)
        {
            DataHelper helper = null;
            DataTable table = null;
            string[] strArray = null;
            string[] strArray2;
            string cmdText = "WAV_RPreviousAllocLoc";
            try
            {
                helper = new DataHelper(sess);
                table = helper.GetTable(CommandType.StoredProcedure, cmdText, new IDataParameter[] { DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum), DataHelper.BuildParameter(sess, "@COMPANY", stCompany), DataHelper.BuildParameter(sess, "@ITEM", stItem), DataHelper.BuildParameter(sess, "@ATTRIBUTE_NUM", iAttributeNum), DataHelper.BuildParameter(sess, "@CONVERTED_QTY_UM", stQtyUM) });
                if (DataManager.IsEmpty(table))
                {
                    return null;
                }
                strArray = new string[table.Rows.Count];
                int num = 0;
                foreach (DataRow row in table.Rows)
                {
                    strArray[num++] = DataManager.GetString(row, "FROM_LOC");
                }
                strArray2 = strArray;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                strArray2 = null;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return strArray2;
        }

        private void GetReplSysValue(Session sess)
        {
            string str = null;
            this.bPickAfterRpln = ConfigValueManager.GetConfigValue(sess, "REPLENISHMENT", "10") == "Y";
            str = ConfigValueManager.GetConfigValue(sess, "REPLENISHMENT", "20");
            if (!StringManager.IsEmpty(str))
            {
                this.iRplnMode = Convert.ToInt32(str);
            }
        }

        public DataTable GetRplnAllocRequest(Session sess, int iWaveNum)
        {
            DataHelper helper = null;
            DataTable table;
            try
            {
                helper = new DataHelper(sess);
                table = helper.GetTable(CommandType.StoredProcedure, "WAV_RplnAllocReq", new IDataParameter[] { DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum), DataHelper.BuildParameter(sess, "@REPLENISHMENT_REQD", "Y") });
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                table = null;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return table;
        }

        private string redirectFromLoc(Session sess, int iWaveNum, string stCompany, string stItem, int iAttrNum, string stInvSts, string stFromLoc, string stFromLPN, string stRedirectLoc)
        {
            DataHelper helper = null;
            string str;
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, "WAV_RedirectFromLoc", new IDataParameter[] { DataHelper.BuildParameter(sess, "@INTERNAL_WAVE_NUM", iWaveNum), DataHelper.BuildParameter(sess, "@COMPANY", stCompany), DataHelper.BuildParameter(sess, "@ITEM", stItem), DataHelper.BuildParameter(sess, "@ATTRIBUTE_NUM", iAttrNum), DataHelper.BuildParameter(sess, "@INVENTORY_STS", stInvSts), DataHelper.BuildParameter(sess, "@FROM_LOC", stFromLoc), DataHelper.BuildParameter(sess, "@FROM_LPN", stFromLPN), DataHelper.BuildParameter(sess, "@REDIRECT_LOC", stRedirectLoc) });
                str = null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                str = MessageManager.MSG_GEN_000 + exception.Message;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return str;
        }

        public string ReplenishByWaveDemand(Session sess, int iWaveNum)
        {
            List<ShipmentDetailDO> openShipmentDetailsInWave = null;
            com.vs.Inventory.BL.AllocationHelper helper = null;
            ItemManager manager = null;
            string str = null;
            string str2 = null;
            DataTable rplnAllocRequest = null;
            try
            {
                openShipmentDetailsInWave = this.GetOpenShipmentDetailsInWave(sess, iWaveNum);
                if ((openShipmentDetailsInWave == null) || (openShipmentDetailsInWave.Count < 1))
                {
                    HistoryManager.SaveProcessHistory(sess, HistoryManager.ProcessType.iReplenishment, HistoryManager.Condition.iInfo, MessageManager.MSG_WAV_010, iWaveNum, null, null, null);
                    return null;
                }
                helper = new com.vs.Inventory.BL.AllocationHelper {
                    bCreateTask = this.bCreateTask
                };
                manager = new ItemManager();
                foreach (ShipmentDetailDO ldo in openShipmentDetailsInWave)
                {
                    str = manager.GetReplenishmentRule(sess, ldo.COMPANY, ldo.ITEM);
                    if (StringManager.IsEmpty(str))
                    {
                        HistoryManager.SaveProcessHistory(sess, HistoryManager.ProcessType.iReplenishment, HistoryManager.Condition.iFailed, MessageManager.MSG_WAV_008, iWaveNum, ldo.ITEM, ldo.COMPANY, null);
                    }
                    else
                    {
                        str2 = helper.AllocateShipmentLine(sess, ldo, str, true);
                        if (!StringManager.IsEmpty(str2))
                        {
                            HistoryManager.SaveProcessHistory(sess, HistoryManager.ProcessType.iReplenishment, HistoryManager.Condition.iFailed, str2, iWaveNum, ldo.ITEM, ldo.COMPANY, null);
                        }
                    }
                }
                rplnAllocRequest = this.GetRplnAllocRequest(sess, iWaveNum);
                if (!DataManager.IsEmpty(rplnAllocRequest))
                {
                    string[] strArray = null;
                    string[] strArray2 = null;
                    string[] strArray3 = null;
                    com.vs.Inventory.BL.InventoryHelper helper3 = new com.vs.Inventory.BL.InventoryHelper();
                    int num = 0;
                    string stItem = null;
                    string stCompany = null;
                    string stWarehouse = null;
                    string stQtyUM = null;
                    string str7 = null;
                    int iAttributeNum = 0;
                    string stFromLoc = null;
                    string stLoc = null;
                    string stFromLPN = null;
                    string stInvSts = null;
                    int @int = 0;
                    int num4 = 0;
                    int num5 = 0;
                    int num6 = 0;
                    InventoryChangeDS item = null;
                    List<InventoryChangeDS> arInvChgDS = null;
                    foreach (DataRow row in rplnAllocRequest.Rows)
                    {
                        int num8;
                        int length;
                        stCompany = DataManager.GetString(row, "COMPANY");
                        stItem = DataManager.GetString(row, "ITEM");
                        iAttributeNum = DataManager.GetInt(row, "ATTRIBUTE_NUM");
                        stQtyUM = DataManager.GetString(row, "CONVERTED_QTY_UM");
                        stWarehouse = DataManager.GetString(row, "WAREHOUSE");
                        @int = DataManager.GetInt(row, "SUM_QTY");
                        stFromLoc = DataManager.GetString(row, "FROM_LOC");
                        stFromLPN = DataManager.GetString(row, "FROM_LPN");
                        str7 = DataManager.GetString(row, "QUANTITY_UM");
                        stInvSts = DataManager.GetString(row, "INVENTORY_STS");
                        num4 = @int;
                        num5 = 0;
                        if (this.iRplnMode == 300)
                        {
                            ItemUnitOfMeasureDO edo = manager.GetParentUM(sess, stItem, stCompany, stQtyUM);
                            if (null != edo)
                            {
                                num6 = edo.CONVERSION_QTY;
                                if ((@int % num6) > 0)
                                {
                                    num4 = ((@int / num6) + 1) * num6;
                                }
                            }
                        }
                        else if (this.iRplnMode == 200)
                        {
                        }
                        strArray = this.getPreviousAllocLoc(sess, iWaveNum, stCompany, stItem, iAttributeNum, stQtyUM);
                        int num7 = 0;
                        if (strArray != null)
                        {
                            num7 += strArray.Length;
                        }
                        strArray2 = this.GetEmptyLocs(sess, stWarehouse, stCompany, stItem, stQtyUM);
                        if (strArray2 != null)
                        {
                            num7 += strArray2.Length;
                        }
                        strArray3 = new string[num7];
                        if (this.bEmptyLocFirst)
                        {
                            num8 = 0;
                            while (num8 < strArray2.Length)
                            {
                                strArray3[num8] = strArray2[num8];
                                num8++;
                            }
                            length = strArray2.Length;
                            num8 = length;
                            while (num8 < num7)
                            {
                                strArray3[num8] = strArray[num8 - length];
                                num8++;
                            }
                        }
                        else
                        {
                            num8 = 0;
                            while (num8 < strArray.Length)
                            {
                                strArray3[num8] = strArray2[num8];
                                num8++;
                            }
                            length = strArray.Length;
                            num8 = length;
                            while (num8 < num7)
                            {
                                strArray3[num8] = strArray2[num8 - length];
                                num8++;
                            }
                        }
                        for (num8 = 0; num8 < strArray3.Length; num8++)
                        {
                            stLoc = strArray3[num8];
                            if (this.bPickAfterRpln)
                            {
                                num = helper3.GetLocAvailableCapacity(sess, stWarehouse, stCompany, stItem, stLoc, str7);
                            }
                            else
                            {
                                num = helper3.GetLocCapacity(sess, stWarehouse, stCompany, stItem, stLoc, str7);
                            }
                            if (num >= num4)
                            {
                                num5 = num4;
                                num4 = 0;
                            }
                            else
                            {
                                continue;
                            }
                            new ReplenishmentRequestDO { ALLOCATED_QTY = num5, ATTRIBUTE_NUM = iAttributeNum, COMPANY = stCompany, FROM_LOC = stFromLoc, FROM_LPN = stFromLPN, INTERNAL_WAVE_NUM = iWaveNum, ITEM = stItem, QUANTITY_UM = str7, TASK_CREATED = this.bCreateTask ? "Y" : "N", TO_LOC = stLoc, USER_STAMP = sess.User, WAREHOUSE = stWarehouse, INVENTORY_STS = stInvSts, DATE_TIME_STAMP = DateTime.Now }.Insert(sess);
                            arInvChgDS = new List<InventoryChangeDS>();
                            if (num5 > @int)
                            {
                                item = new InventoryChangeDS {
                                    ATTRIBUTE_NUM = iAttributeNum,
                                    COMPANY = stCompany,
                                    LOCATION = stFromLoc,
                                    LPN = stFromLPN,
                                    INVENTORY_STS = stInvSts,
                                    ITEM = stItem,
                                    QUANTITY = num5 - @int,
                                    QUANTITY_UM = str7,
                                    WAREHOUSE = stWarehouse
                                };
                                if (this.bCreateTask)
                                {
                                    item.OP_ALLOCQTY = "+";
                                    item.OP_INTRANQTY = "";
                                    item.OP_ONHANDQTY = "";
                                }
                                else
                                {
                                    item.OP_ALLOCQTY = "";
                                    item.OP_INTRANQTY = "";
                                    item.OP_ONHANDQTY = "-";
                                }
                                arInvChgDS.Add(item);
                            }
                            item = new InventoryChangeDS {
                                ATTRIBUTE_NUM = iAttributeNum,
                                COMPANY = stCompany,
                                LOCATION = stLoc,
                                INVENTORY_STS = stInvSts,
                                ITEM = stItem,
                                QUANTITY = num5,
                                QUANTITY_UM = str7,
                                WAREHOUSE = stWarehouse
                            };
                            if (this.bCreateTask)
                            {
                                item.OP_ALLOCQTY = "";
                                item.OP_INTRANQTY = "+";
                                item.OP_ONHANDQTY = "";
                            }
                            else
                            {
                                item.OP_ALLOCQTY = "";
                                item.OP_INTRANQTY = "";
                                item.OP_ONHANDQTY = "+";
                            }
                            arInvChgDS.Add(item);
                            item = new InventoryChangeDS {
                                ATTRIBUTE_NUM = iAttributeNum,
                                COMPANY = stCompany,
                                LOCATION = stLoc,
                                INVENTORY_STS = stInvSts,
                                ITEM = stItem,
                                QUANTITY = @int,
                                QUANTITY_UM = str7,
                                WAREHOUSE = stWarehouse
                            };
                            if (this.bCreateTask)
                            {
                                item.OP_ALLOCQTY = "+";
                                item.OP_INTRANQTY = "";
                                item.OP_ONHANDQTY = "";
                            }
                            else
                            {
                                item.OP_ALLOCQTY = "";
                                item.OP_INTRANQTY = "";
                                item.OP_ONHANDQTY = "-";
                            }
                            arInvChgDS.Add(item);
                            new com.vs.Inventory.BL.InventoryHelper().AdjustInventory(sess, arInvChgDS);
                            this.redirectFromLoc(sess, iWaveNum, stCompany, stItem, iAttributeNum, stInvSts, stFromLoc, stFromLPN, stLoc);
                            if (num4 <= 0)
                            {
                                break;
                            }
                        }
                    }
                }
                return null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return (MessageManager.MSG_GEN_000 + exception.Message);
            }
        }

        public List<InventoryTransDS> ReplenishItem(Session sess, InventoryDS invDS, string stAllocRule, string stToLoc)
        {
            return null;
        }
    }
}

